package club.banyuan.hrms.servlet;

import club.banyuan.hrms.dao.DepartmentDao;
import club.banyuan.hrms.dao.impl.DepartmentDaoImpl;
import club.banyuan.hrms.domain.Department;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class DepartmentDeleteServlet extends HttpServlet {
    private DepartmentDao departmentDao;

    @Autowired
    public DepartmentDeleteServlet(DepartmentDao departmentDao) {
        this.departmentDao = departmentDao;
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        String data = request.getParameter("ids");
        String[] ids = data.split(",");
        Integer[] integerIds = new Integer[ids.length];
        for (int i = 0; i < ids.length; i++) {
            integerIds[i] = Integer.parseInt(ids[i]);
        }

        departmentDao.deleteDepartment(integerIds);

//        List<Department> departments = departmentDao.getDepartments();
//        for (Department department : departments) {
//            for (String id : ids) {
//                if (department.getId() == Integer.parseInt(id)) {
//                    departments.remove(department);
//                }
//            }
//        }

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("code", 0);
        resultMap.put("massage", "删除成功");
        response.getWriter().println(JSONObject.toJSONString(resultMap));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
